<?php
add_action('init', 'add_shortcode_button');
add_shortcode('video', 	'addSC_Youtube');  
// add custom button
function add_shortcode_button() {
	if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) {
		return;
	}
	if ( get_user_option('rich_editing') == 'true' ) {
		add_filter('mce_external_plugins', 'add_spa_shortcode');
		add_filter('mce_buttons', 'register_button_shortcode');
	}
}
function register_button_shortcode($buttons) {
	array_push($buttons, "", "spa_video");
	return $buttons;
}
function add_spa_shortcode($plugin_array) {
	$plugin_array['spa_video']		= FOLDER_JS .'/shortcode-video.js';
	return $plugin_array;
}
function addSC_Youtube($atts, $content = null) {
	$rd = generate_random_string(4); //rand(5, 15);
	extract(shortcode_atts(array( "id" => '', 'width' => '', 'height' => '' ), $atts));
	//return '<input type="text" id="ans_'. $id .'" />';		
	return '<div class="spa_video">
	<div id="spa_video'. $rd .'">This text will be replaced</div>
	<script type="text/javascript">
		jwplayer("spa_video'. $rd .'").setup({
		controlbar: "bottom",
		file: "'. $id .'",
		stretching: "exactfit",
		flashplayer: "'. TEMPLATE_URL .'/mediaplayer/player.swf",
		volume: 80,
		width:'. $width .',
		height:'. $height .'
		});
	</script>
	</div>';	
}
function generate_random_string($name_length = 8) {
	$alpha_numeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	return substr(str_shuffle($alpha_numeric), 0, $name_length);
}
?>